﻿using System.Collections.Generic;
using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;
using Fighter.Masters.ModuleData;
using Fighter.Fights.ModuleData;
using System.Windows.Forms;
using Fighter.Fights.Libraries;
using Fighter.Fights.Reports.DataSet;
using System;
using System.Linq;

namespace Fighter.Fights.Reports
{
    public partial class EntryList : ApplicationLibrary.DBManager.ReportForm
    {
        DRAthlete drAthlete = null;
        DSEntryList dsEntryList = null;
        DRClub drClub = null;
        RRCoachForClub rrCoachForClub = null;
        DRFightActive drFightActive = null;
        

        Dictionary<string, string> clubList = new Dictionary<string, string>();
        Dictionary<string, SortedList<string, string>> coachList = new Dictionary<string, SortedList<string, string>>();
        Dictionary<string, SortedList<string, string>> athleteList = new Dictionary<string, SortedList<string, string>>();

        public EntryList()
            : base("EntryList")
        {
            InitializeComponent();
        }

        protected override void OnBatchExecute(string reportName)
        {
            if (!LoadData())
            {
                MessageBox.Show(Properties.Resources.Msg_NoDataFound, Properties.Resources.M_Warning, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            //ShowReport(new RPTBalance(), dsBalance);
        }

        protected override void  OnAttachData()
        {
            drAthlete = new DRAthlete();
            drClub = new DRClub();
            rrCoachForClub = new RRCoachForClub();
            dsEntryList = new DSEntryList();
            drFightActive = new DRFightActive();
        }

        private bool LoadData()
        {
            int currFight = 0;
            dsEntryList.Clear();
            if (drFightActive.Find())
                currFight = drFightActive.GetValue<int>(FG_Fight.ID);

            QueryBuilder qb = new QueryBuilder();
            SqlABParameter p1 = new SqlABParameter("@p1", FG_Registration.Fight);
            p1.Value = 

            qb.AddSelect();
            qb.AddFrom(FG_Registration.Name);
            qb.AddCompare<int>(FG_Registration.Fight, currFight);

            try
            {
                using (SqlABCommand scc = new SqlABCommand(base.providerType, GlobalInfo.SqlConnection))
                {
                    scc.CommandText = qb.Query;

                    SqlABDataReader dr = scc.ExecuteReader();
                    while (dr.Read())
                    {
                        string mail = dr.GetValue<string>(FG_Registration.Mail);
                        if (!clubList.ContainsKey(mail))
                        {
                            System.Diagnostics.Debug.WriteLine(mail);
                            drClub.Find(mail);
                            string clubDesc = string.Format("{0} ( {1} )", drClub.GetValue<string>(FG_Club.Club), drClub.GetValue<string>(FG_Club.City));
                            clubList.Add(mail, clubDesc);
                            AddCoach(mail);
                        }
                        if (drAthlete.Find(dr.GetValue<int>(FG_Registration.Athlete)))
                        {
                            if (!athleteList.ContainsKey(mail))
                                athleteList.Add(mail, new SortedList<string, string>());
                            System.Diagnostics.Debug.WriteLine(drAthlete.GetValue<string>(FG_Athlete.FirstName));
                            athleteList[mail].Add(drAthlete.GetValue<string>(FG_Athlete.FirstName), drAthlete.GetValue<string>(FG_Athlete.FirstName));
                        }
                    }
                    dr.Close();
                }
            }
            catch (Exception e)
            {

            }

            foreach (KeyValuePair<string,string>  club in clubList)
            {
                DSEntryList.AggregatorRow row = dsEntryList.Aggregator.NewAggregatorRow();
                row.Mail = club.Key;
                row.Description = club.Value;
                dsEntryList.Aggregator.AddAggregatorRow(row);

                int max = int.MinValue;
                if (coachList.ContainsKey(club.Key))
                    max = Math.Max(max, coachList[club.Key].Count);

                if (athleteList.ContainsKey(club.Key))
                    max = Math.Max(max, athleteList[club.Key].Count);

                for (int t = 0; t < max; t++)
                {
                    DSEntryList.EntryListRow list = dsEntryList.EntryList.NewEntryListRow();
                    list.Mail = club.Key;
                    if (coachList.ContainsKey(club.Key) && t < coachList[club.Key].Count)
                        list.Coach = coachList[club.Key].Values[t];
                    else
                        list.Coach = string.Empty;

                    if (athleteList.ContainsKey(club.Key) && t < athleteList[club.Key].Count)
                        list.Athlete = athleteList[club.Key].Values[t];
                    else
                        list.Athlete = string.Empty;
                    
                    dsEntryList.EntryList.AddEntryListRow(list);
                }
            }
            ShowReport(new RPTEntryList(), dsEntryList);
            return true;
        }

        private void AddCoach(string mail)
        {
            if (rrCoachForClub.Find(mail))
            {
                for (int t = 0; t < rrCoachForClub.Count; t++)
                {
                    if (!coachList.ContainsKey(mail))
                        coachList.Add(mail, new SortedList<string, string>());
                    string firstName = rrCoachForClub.GetValue<string>(FG_Coach.FirstName, t);
                    if (!coachList[mail].ContainsKey(firstName))
                        coachList[mail].Add(rrCoachForClub.GetValue<string>(FG_Coach.FirstName, t), rrCoachForClub.GetValue<string>(FG_Coach.FirstName, t));
                }
            }
        }
    }
}
